//
//  MBVipProfileView.m
//  Meboo
//
//  Created by kyo on 2024/3/12.
//

#import "MBVipProfileView.h"
#import "MBChatListViewController.h"
#import "MBEditProfileViewController.h"
#import "KLVipViewController.h"
#import "MBHomePPVPopView.h"
@interface MBVipProfileView ()
//恢复购买
@property (nonatomic, strong) UIButton *resumeBtn;
@property(nonatomic,strong) UIButton *backBtn;
@property(nonatomic,strong) UIButton *messageBtn;


@property(nonatomic,strong) UIImageView *vipProfileBackImageView;
@property(nonatomic,strong) UIButton *editBtn;
@property(nonatomic,strong) UILabel *completionLab;



@property(nonatomic,strong) UIImageView *vipBackImageView;
@property(nonatomic,strong) UIImageView *crownImageView;
@property(nonatomic,strong) UIImageView *arrowImageView;
@property(nonatomic,strong) UILabel *vipLab;
@property(nonatomic,strong) MBHomePPVPopView *ppvPopView;
@end

@implementation MBVipProfileView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}


#pragma mark - Public Metohd
- (void)resetData {
    self.PPView.titleLab.text = [NSString stringWithFormat:@"%ld",[MBUserAccount defaultAccount].checkPPNum];
    self.PVView.titleLab.text = [NSString stringWithFormat:@"%ld",[MBUserAccount defaultAccount].checkPVNum];
}

#pragma mark - Action
- (void)messageAction {
    NSArray *displayConversationTypeArray = @[@(ConversationType_PRIVATE)];
    //        RCConversationListViewController *conversationListVC = [[RCConversationListViewController alloc] initWithDisplayConversationTypes:displayConversationTypeArray
    //                                                                                               collectionConversationType:nil];
    MBChatListViewController *conversationListVC = [[MBChatListViewController alloc] initWithDisplayConversationTypes:displayConversationTypeArray
                                                                                           collectionConversationType:nil];
    [conversationListVC setConversationAvatarStyle:RC_USER_AVATAR_CYCLE];
    [self.currentViewController.navigationController pushViewController:conversationListVC animated:YES];
}

- (void)backAction {
    [self.currentViewController.navigationController popViewControllerAnimated:YES];
}

- (void)editProfileAction {
    [self.currentViewController.navigationController pushViewController:[MBEditProfileViewController new] animated:YES];
}

- (void)vipAction {
    [self.currentViewController.navigationController pushViewController:[KLVipViewController new] animated:YES];
}

- (void)clickPP {
    self.ppvPopView.hidden = NO;
    self.ppvPopView.isVideo = NO;
}

- (void)clickPV {
    self.ppvPopView.hidden = NO;
    self.ppvPopView.isVideo = YES;
}

#pragma mark - UI
- (void)setupUI {
    self.backgroundColor = UIColor.clearColor;
    
//    [self addSubview:self.backBtn];
//    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(MBWidthScale(20));
//        make.top.mas_equalTo(StatusBarHeight+MBHeightScale(12));
//        make.width.height.mas_equalTo(MBHeightScale(20));
//    }];
    
    [self addSubview:self.messageBtn];
    [self.messageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.backBtn);
        make.top.mas_equalTo(StatusBarHeight+MBHeightScale(9));
        make.right.mas_equalTo(MBWidthScale(-20));
//        make.width.mas_equalTo(MBWidthScale(37));
//        make.height.mas_equalTo(MBHeightScale(26));
    }];
    
    [self addSubview:self.resumeBtn];
    [self.resumeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(20));
        make.centerY.equalTo(self.messageBtn);
    }];
    
    [self addSubview:self.iconImageView];
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.messageBtn.mas_bottom).offset(MBHeightScale(16));
        make.centerX.equalTo(self);
        make.width.height.mas_equalTo(MBHeightScale(83));
    }];
    
    [self addSubview:self.vipImageView];
    [self.vipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView).offset(MBWidthScale(-6));
        make.top.equalTo(self.iconImageView).offset(MBHeightScale(-10));
        make.width.height.mas_equalTo(MBHeightScale(32));
    }];
    
    [self addSubview:self.nameLab];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImageView.mas_bottom).offset(MBHeightScale(10));
        make.left.mas_equalTo(MBWidthScale(20));
        make.right.mas_equalTo(MBWidthScale(-20));
    }];
    
    [self addSubview:self.vipProfileBackImageView];
    [self.vipProfileBackImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(MBHeightScale(165));
        make.bottom.mas_equalTo(MBHeightScale(-84));
        make.left.mas_equalTo(MBWidthScale(15));
        make.right.mas_equalTo(MBWidthScale(-15));
    }];
    
    [self.vipProfileBackImageView addSubview:self.completionLab];
    [self.completionLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(MBHeightScale(15));
        make.left.mas_equalTo(MBWidthScale(5));
        make.right.mas_equalTo(MBWidthScale(-120));
    }];
    
    [self.vipProfileBackImageView addSubview:self.editBtn];
    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(MBHeightScale(-15));
        make.height.mas_equalTo(MBHeightScale(35));
        make.left.mas_equalTo(MBWidthScale(17));
        make.right.mas_equalTo(MBWidthScale(-115-17));
    }];
    
    [self.vipProfileBackImageView addSubview:self.rateLab];
    [self.rateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.completionLab.mas_bottom);
        make.bottom.equalTo(self.editBtn.mas_top);
        make.left.mas_equalTo(MBWidthScale(5));
        make.right.mas_equalTo(MBWidthScale(-120));
    }];
    
    [self addSubview:self.vipBackImageView];
    [self.vipBackImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(MBWidthScale(115));
        make.top.bottom.right.equalTo(self.vipProfileBackImageView);
    }];
    
    [self.vipBackImageView addSubview:self.crownImageView];
    [self.crownImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(MBHeightScale(20));
        make.centerX.equalTo(self.vipBackImageView);
        make.width.mas_equalTo(MBWidthScale(75));
        make.height.mas_equalTo(MBWidthScale(75)/64*58);
    }];
    
//    [self.vipBackImageView addSubview:self.arrowImageView];
//    [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.mas_equalTo(MBHeightScale(-23));
//        make.centerX.equalTo(self.vipBackImageView);
//        make.width.height.mas_equalTo(MBHeightScale(20));
//    }];
    
    [self.vipBackImageView addSubview:self.vipLab];
    [self.vipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(MBHeightScale(-20));
        make.left.mas_equalTo(MBWidthScale(5));
        make.right.mas_equalTo(MBWidthScale(-5));
    }];
    
    [self addSubview:self.PPView];
    [self.PPView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.vipProfileBackImageView.mas_bottom).mas_equalTo(MBHeightScale(10));
        make.bottom.equalTo(self);
        make.left.mas_equalTo(MBWidthScale(15));
        make.right.equalTo(self.mas_centerX).offset(MBWidthScale(-5));
    }];
    
    [self addSubview:self.PVView];
    [self.PVView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.vipProfileBackImageView.mas_bottom).mas_equalTo(MBHeightScale(10));
        make.bottom.equalTo(self);
        make.right.mas_equalTo(MBWidthScale(-15));
        make.left.equalTo(self.mas_centerX).offset(MBWidthScale(5));
    }];
   
    [kKeyWindow addSubview:self.ppvPopView];
}

#pragma mark - Setter && Getter

- (UIImageView *)vipProfileBackImageView {
    if (!_vipProfileBackImageView) {
        _vipProfileBackImageView = [UIImageView new];
        _vipProfileBackImageView.image = ImageWithName(@"vipprofile_back");
        _vipProfileBackImageView.userInteractionEnabled = YES;
    }
    return _vipProfileBackImageView;
}

- (UILabel *)completionLab {
    if (!_completionLab) {
        _completionLab = [UILabel new];
        _completionLab.textAlignment = NSTextAlignmentCenter;
        _completionLab.font = PDBoldScale(16);
        _completionLab.textColor = HEXS(@"B92176");
        _completionLab.text = @"Data completion rate";
    }
    return _completionLab;
}

- (UILabel *)rateLab {
    if (!_rateLab) {
        _rateLab = [UILabel new];
        _rateLab.textAlignment = NSTextAlignmentCenter;
        _rateLab.font = MBHelFontSizeScale(40);
        _rateLab.textColor = HEXS(@"B92176");
        _rateLab.text = @"25%";
    }
    return _rateLab;
}

- (UIButton *)editBtn {
    if (!_editBtn) {
        _editBtn = [[UIButton alloc] init];
        [_editBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_editBtn setTitle:@"Click to edit your profile" forState:0];
        _editBtn.titleLabel.font = PDMediumScale(15);
        [_editBtn setTitleColor:UIColor.whiteColor forState:0];
        _editBtn.layer.cornerRadius = MBHeightScale(9);
        [_editBtn setBackgroundColor:HEXS(@"B92176")];
        [_editBtn addTarget:self action:@selector(editProfileAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _editBtn;
}

- (UIImageView *)vipBackImageView {
    if (!_vipBackImageView) {
        _vipBackImageView = [UIImageView new];
        _vipBackImageView.image = ImageWithName(@"vipprofile_vip_back");
        _vipBackImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(vipAction)];
         [_vipBackImageView addGestureRecognizer:singleTap];
    }
    return _vipBackImageView;
}

- (UIImageView *)crownImageView {
    if (!_crownImageView) {
        _crownImageView = [UIImageView new];
        _crownImageView.image = ImageWithName(@"vipprofile_back_vip_crown");
    }
    return _crownImageView;
}

- (UIImageView *)arrowImageView {
    if (!_arrowImageView) {
        _arrowImageView = [UIImageView new];
        _arrowImageView.image = ImageWithName(@"vipprofile_back_vip_arrow");
    }
    return _arrowImageView;
}

- (UILabel *)vipLab {
    if (!_vipLab) {
        _vipLab = [UILabel new];
        _vipLab.textAlignment = NSTextAlignmentCenter;
        _vipLab.numberOfLines = 2;
        _vipLab.font = PDBoldScale(16);
        _vipLab.textColor = HEXS(@"C07013");
        _vipLab.text = @"Member\nactivation";
    }
    return _vipLab;
}


- (UIButton *)messageBtn {
    if (!_messageBtn) {
        _messageBtn = [[UIButton alloc] init];
        [_messageBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_messageBtn setBackgroundImage:ImageWithName(@"common_chat") forState:0];
        [_messageBtn addTarget:self action:@selector(messageAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _messageBtn;
}

- (UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [UIButton new];
        [_backBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_backBtn setBackgroundImage:ImageWithName(@"back_arrow_black") forState:0];
        [_backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (UIButton *)resumeBtn {
    if (!_resumeBtn) {
        _resumeBtn = [[UIButton alloc] init];
        _resumeBtn.titleLabel.font = MBFontSizeScale(7);
        [_resumeBtn setTitleColor:HEXS(@"000000") forState:0];
        [_resumeBtn setTitle:@"restore purchase" forState:0];
//        [_resumeBtn addTarget:self action:@selector(resumeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _resumeBtn;
}

- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [UIImageView new];
        _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
        _iconImageView.layer.cornerRadius = MBHeightScale(83/2);
        _iconImageView.layer.masksToBounds = YES;
        _iconImageView.layer.borderColor = HEXS(@"FFBC1F").CGColor;
        _iconImageView.layer.borderWidth = 2;
        _iconImageView.image = ImageWithName(@"appLogo");
        MBUserAccount *account = [MBUserAccount defaultAccount];
        if (account.avatarUrl.length > 0) {
            if (![account.avatarUrl containsString:@"http"]) {
                account.avatarUrl = [NSString stringWithFormat:@"https://storage.googleapis.com/meboo-online-b68e7.appspot.com%@",account.avatarUrl];
                [MBUserAccount saveAccount:account];
            }
            [_iconImageView sd_setImageWithURL:[NSURL URLWithString:account.avatarUrl]];
        }
    }
    return _iconImageView;
}

- (UILabel *)nameLab {
    if (!_nameLab) {
        _nameLab = [UILabel new];
        _nameLab.textAlignment = NSTextAlignmentCenter;
        _nameLab.font = PDSemiBoldScale(18);
        _nameLab.textColor = HEXS(@"000000");
        _nameLab.text = [NSString stringWithFormat:@"%@,%@",[MBUserAccount defaultAccount].nickName,[MBUserAccount defaultAccount].age];
        if ([[MBUserAccount defaultAccount].age integerValue] < 18) {
            _nameLab.text = [NSString stringWithFormat:@"%@,18",[MBUserAccount defaultAccount].nickName];
        }
        
    }
    return _nameLab;
}

- (UIImageView *)vipImageView {
    if (!_vipImageView) {
        _vipImageView = [UIImageView new];
        _vipImageView.image = ImageWithName(@"modellist_vip_icon");
        _vipImageView.hidden = YES;
    }
    return _vipImageView;
}


- (MBVipProfilePPView *)PPView {
    if (!_PPView) {
        _PPView = [MBVipProfilePPView new];
        UITapGestureRecognizer *ppTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickPP)];
        [_PPView addGestureRecognizer:ppTap];
    }
    return _PPView;
}

- (MBVipProfilePVView *)PVView {
    if (!_PVView) {
        _PVView = [MBVipProfilePVView new];
        UITapGestureRecognizer *pvTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickPV)];
        [_PVView addGestureRecognizer:pvTap];
    }
    return _PVView;
}

- (MBHomePPVPopView *)ppvPopView {
    if (!_ppvPopView) {
        __WeakSelf
        _ppvPopView = [[MBHomePPVPopView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _ppvPopView.payBlock = ^(BOOL success) {
            if (success) {
                //支付成功发送通知---更新ui
                [weakSelf resetData];
            }
        };
        _ppvPopView.hidden = YES;
    }
    return _ppvPopView;
}
@end
